#include "mainwindow.h"
#include<QSqlDatabase>
#include <QDebug>
#include <QMessageBox>
#include "QSqlError"
#include "QSqlQuery"
#include "QPushButton"
#include "QGridLayout"
#include "QWidget"
#include "user_mapper.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), page(1)
{
    // this->initDataBase();
    qDebug() << QSqlDatabase::drivers();
    this->setCentralWidget(new QWidget(this));
    auto* mainLayout = new QGridLayout(this->centralWidget());
    auto* btn = new QPushButton("select", this);
    mainLayout->addWidget(btn, 0,0);
    UserMapper* userMapper = new UserMapper();
    // userMapper->setDatabaseName("nas.maotao.mysql.douyin_db");
    userMapper->setDatabaseName("douyin_db")->setHost("192.168.1.212")->setUserName("root")->setPassword("twh186915");
    connect(btn, &QPushButton::clicked, this, [userMapper, this](){
        std::vector<User> result = userMapper->SelectUserInfoByPage(5, this->page);
        this->page++;
        for(auto r : result){

            qInfo() << r.userId << " " << r.nickName;
        }

    });


}

MainWindow::~MainWindow() {
    if(db.isOpen()) {
        db.close();
    }
}

void MainWindow::initDataBase(){
    db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName("nas.maotao.mysql.douyin_db");
    if(!db.open()){
        QMessageBox::information(this,"error", "failed");
        qCritical() << db.lastError().text();
    }
}
